#include "common/common.h"
#include "Core/TCFixedString.h"
#include "memory/pilotblockallocator.h"
#include "information/piloerrorinformations.h"
#include "information/piloenvironmentvariableinformations.h"
#include "Core/CAbstractThread.h"

#include "testlibpilo.h"



#include "string/stringoutput.h"
#include "Core/TCSimpleDynamicArray.h"
#include "Core/TCSimpleDynamicSortedArray.h"
#include "Core/TCFixedBuffer.h"



using namespace PILO::Core::Containers;
using namespace PILO::Core::String;
using namespace PILO::Core::Memory;
using namespace PILO::Core::Information;
using namespace PILO::Core::Thread;

M_ATOMIC UINT64 gcount = 0;

int main(int , char *[])
{
    //Common
    test_atomic_operation_functional(1,1);

    //Containers
    test_TCSimpleDynamicArray_Functional(2,1);
    test_TCSimpleDynamicSortedArray_Functional(3,1);

    //IPC
    test_CThreadMutex_Functional(4,1);
    test_CAutoMutexLocker_Functional(5,1);

    //Strings
    test_stringmanipulations_Functional(6,1);
    test_TCFixedString_Functional(7,1);
    
   //Thread
    test_atomictypes_Functional(1,1);
    test_CAbstractThread_Functional(8,1);

//     TCFixedString_FunctionalTest(1,1);
//     pilostringmanipulations_FunctionalTest(2,1);
// 
// 
//     testTCFixedBufferFunctional(5,1);
//     test_atomic_operation_functional(6,1);
//     testTCFixedRingBufferFunctional(7,1);
//     test_CAbstractThread_Functional(8,1);

   //   pilostringmanipulations_FunctionalTest(1, 0);
   //   TCFixedString_FunctionalTest(2, 0);
       // PILOTSimpleDynamicArray_FunctionalTest(3, 0);
          // PILOTSimpleDynamicSortedArray_FunctionalTest(4,1);
          // PILOTSimpleDynamicObjectArray_FunctionalTest(5,1);

    //  PILOTEnvironmentVariableExtractor<wchar_t> extractor;
     // extractor.extractAllEnvironmentVariables();
     // extractor.consoleDebug();



//    SetLastError(2);
//    errno = EFAULT;
 //   TCFixedString<wchar_t, 512> msgerr;

//PILOPrintf("String data cat is %d\n", msgerr.capacity());
//    PILOTGetLastSystemError(msgerr, EBADF);
  


/*
    for (int i=0; i< 50; i++)
    {
        if (msgerr.data()[i] != 0 ) {
            PILOPrintf(L"[%x]", (unsigned short) msgerr.data()[i]);
        }
    }
    PILOPrintf(L"\n");
    msgerr.updateSize();
*/

//PILOPrintf(L"%d", msgerr.size());

//    msgerr.output();


/*
    PILOTBlockAllocator<info_t,4> allocator;

    void * res;
    info_t * infop;

    res = allocator.alloc(1);
    if (res == 0) {
        printf("ALLOC Failed\n");
        ASSERT(0);
    }
    infop = (info_t *) res;
    infop->id = 1;
    PILOTStringCopy(infop->name, 11,  "0123456789", 10);
    PILOPrintf("1 id is %d, name is %s\n", infop->id, infop->name);


    res = allocator.alloc(1);
    if (res == 0) {
        printf("ALLOC Failed\n");
        ASSERT(0);
    }
    infop = (info_t *) res;
    infop->id = 1;
    PILOTStringCopy(infop->name, 12,  "1123456789", 10);
    PILOPrintf("2 id is %d, name is %s\n", infop->id, infop->name);

    res = allocator.alloc(1);
    if (res == 0) {
        printf("ALLOC Failed\n");
        ASSERT(0);
    }
    infop = (info_t *) res;
    infop->id = 1;
    PILOTStringCopy(infop->name, 13,  "2123456789", 10);
    PILOPrintf("3 id is %d, name is %s\n", infop->id, infop->name);

    res = allocator.alloc(1);
    if (res == 0) {
        printf("ALLOC Failed\n");
        ASSERT(0);
    }
    infop = (info_t *) res;
    infop->id = 1;
    PILOTStringCopy(infop->name, 14,  "3123456789", 10);
    PILOPrintf("4 id is %d, name is %s\n", infop->id, infop->name);

    allocator.free(infop);

    error_number_t err = PILOGetLastSystemErrorNumber();

    tchar_t buffer[MC_SYSTEM_ERROR_MESSAGE_MAX_CHAR];
    buffer[0] = 0;
 
    PILOGetLastSystemErrorMessage(buffer, MC_SYSTEM_ERROR_MESSAGE_MAX_CHAR, 1);

    PILOPrintf(L"error is [%s] sizeof is %d\n", buffer,sizeof(size_t));
*/
	getchar();

    return 0;
}
